//
//  SEHospitalizeOrderImageCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/17.
//

import UIKit

class SEHospitalizeOrderImageCell: UITableViewCell {

    static let className = "SEHospitalizeOrderImageCell"
    static let reuseId = className

    private let collectionViewItemWidth = (UIScreen.screenWidth - 50.0 - 20) / 3.0
    private let collectionViewItemHeight = (UIScreen.screenWidth - 50.0 - 20) / 3.0

    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.register(SEImageCell.nibForCell(), forCellWithReuseIdentifier: SEImageCell.reuseIdentifier())
        }
    }
    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint! {
        didSet {
            collectionViewHeight.constant = collectionViewItemHeight
        }
    }

    var viewModel = SEHospitalizeOrderDetailViewModel()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateCell(_ viewModel: SEHospitalizeOrderDetailViewModel) {
        self.viewModel = viewModel
        collectionView.reloadData()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension SEHospitalizeOrderImageCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.numberOfItemsForImgCv
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEImageCell.reuseIdentifier(), for: indexPath) as! SEImageCell
        cell.update(with: viewModel.infoViewModel, indexPath: indexPath, isCanEdit: false, deleteImage: nil)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionViewItemWidth, height: collectionViewItemHeight)
    }
}
